<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;


use app\common\model\SysConfig;

/**
 * 会员逻辑
 */
class Appconfig extends AdminBase
{



    /**
     * 获取会员列表
     */
    public function getConfigList($data = [])
    {
//        $action=request()->action();
//       switch ($action){
//           case 'configList':
//               $config_id=1;
//               break;
//           case 'about':
//               $config_id=7;
//               break;
//           case 'about':
//               $config_id=7;
//               break;
//           default:
//               $config_id=1;
//               break;
//       }
        $config_bean=$this->modelSysConfig->where(array('config_id'=>$data['id']))->find();
        $config_list =$this->findChildrenConfigList($data['id']);
        if(count($config_list)>0){
            foreach ($config_list as $key=>$item){
                if($item['type']=='radio'){
                    $config_list[$key]['value']=json_decode($item['value']);
                }
                if($item['type']=='checkbox'){
                    $config_list[$key]['value']=json_decode($item['value']);
                }
                if($item['type']=='number_group'){
                    $config_list[$key]['value']=json_decode($item['value'],true);
                    foreach ($config_list[$key]['value'] as $k=>$v){
                        $config_list[$key]['value'][$k]['value']=intval($v['value']);
                    }
                }
                if($item['type']=='number'){
                    $config_list[$key]['value']=intval($item['value']);
                }
            }
            $config_tree=buildListToTree('config_id',$config_list,$data['id']);
           // dd(json_decode(json_encode($config_tree)),true);
            return ['data'=>$config_tree,'config_bean'=>$config_bean];
        }else{
            return ['data'=>[]];
        }

    }

    //获取下级所有
    private function findChildrenConfigList($parent_id){
        $config_list =$this->modelSysConfig->where(array('parent_id'=>$parent_id))->select();
        if(empty($config_list)){
            return null;
        }
        foreach ($config_list as $key=>$item){
            $list=$this->findChildrenConfigList($item['config_id']);
            if(!empty($list)){
                $config_list=array_merge($config_list,$list);
            }
        }

        return $config_list;
    }


    public function  configEdit($data){

        foreach ($data as $key=>$bean){
            if(empty($info)){
                $info=SysConfig::get(['code'=>$key]);
            }
            SysConfig::update(['value'=>$bean],['code'=>$key]);

        }
        cache('sms_config',\app\common\model\SysConfig::where('1=1')->column('code,value'));
        switch ($info['parent_id']){
            case 1:
                $url = url('configList');
                break;
            case 200:
            case 201:
            case 202:
            case 203:
            case 205:
            case 206:
                $url = url('financial_setting');
                break;

            case 5:
                $url = url('against_setting');
                break;
            case 8:
                $url = url('bbs_setting');
                break;
            case 12:
                $url = url('king_action');
                break;
            case 60367:
            case 60373:
            case 60376:
                $url = url('Interface_setting');
                break;
            default:
                $url = url('configList');
                break;
        }

        return [RESULT_SUCCESS, '操作成功', $url];
    }


    public function reg_agreement_set($data,$url){

        SysConfig::where(['config_id'=>$data['config_id']])->update(['value'=>$data['content']]);
        cache('sms_config',\app\common\model\SysConfig::where('1=1')->column('code,value'));
       // dd(cache('agreement'));
//        $url=url('agreement');
        return [RESULT_SUCCESS, '操作成功', $url];
    }



}
